/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package cardgame;

import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JPanel;
import cardgame.game.Game;
import cardgame.game.commands.*;

/**
 *
 * @author Carl
 */
public class PanGame extends javax.swing.JPanel{

    public PanGame(Game game, CommandSender commandSender, PanCardInfo panCardInfo, final PanMessage panMessage){
        initComponents();
        this.game = game;
        this.commandSender = commandSender;
        cardChooserManager = new CardChooserManager(){

            @Override
            protected void start(CardChooser cardChooser){
                super.start(cardChooser);
                if(activeCardChooser.isCancelable()){
                    btnCancelCardChooser.setVisible(true);
                }
                btnEndTurn.setEnabled(false);
            }

            @Override
            public void stop(){
                if(activeCardChooser.isCancelable()){
                    btnCancelCardChooser.setVisible(false);
                }
                btnEndTurn.setEnabled(true);
                super.stop();
            }

            @Override
            public void setMessage(String text){
                panMessage.setMessage((text != null)?text:"");
            }
        };
        btnCancelCardChooser.addActionListener(new ActionListener(){

            @Override
            public void actionPerformed(ActionEvent evt){
                cardChooserManager.stop();
            }
        });
        playerCardFrontend1 = new PlayerCardFrontend(commandSender, cardChooserManager, this, panCardInfo);
        playerCardFrontend2 = new PlayerCardFrontend(commandSender, cardChooserManager, this, panCardInfo);
    }
    private final int padding = 10;
    private final int cardWidth = 40;
    private final int cardHeight = 60;
    private Game game;
    private CommandSender commandSender;
    private CardChooserManager cardChooserManager;
    private PlayerCardFrontend playerCardFrontend1;
    private PlayerCardFrontend playerCardFrontend2;
    private int playerIndex;
    
    public void setPlayerIndex(int playerIndex){
        this.playerIndex = playerIndex;
        int enemyPlayerIndex = ((playerIndex == 0)?1:0);
        playerCardFrontend1.setPlayerIndex(playerIndex);
        playerCardFrontend2.setPlayerIndex(enemyPlayerIndex);
        initialiseCardPanel_Deck(playerCardFrontend1, panHand1);
        initialiseCardPanel_Deck(playerCardFrontend2, panHand2);
        initaliseCardPanels_Hand(playerCardFrontend1, playerIndex, 5);
        initaliseCardPanels_Hand(playerCardFrontend2, enemyPlayerIndex, 5);
        initialiseCardPanel_Graveyard(playerCardFrontend1, panField1, padding);
        initialiseCardPanel_Graveyard(playerCardFrontend2, panField2, padding + cardHeight + padding);
        initaliseCardPanels_Monsters(playerCardFrontend1, playerIndex, 0);
        initaliseCardPanels_Monsters(playerCardFrontend2, enemyPlayerIndex, 0);
        initaliseCardPanels_ManaPool(playerCardFrontend1, playerIndex, 0);
        initaliseCardPanels_ManaPool(playerCardFrontend2, enemyPlayerIndex, 0);
        btnCancelCardChooser.setVisible(false);
    }
    
    public void update(){
        boolean isOwnTurn = (playerIndex == game.getCurrentPlayerIndex());
        lblCurrentPlayer.setText("<html>Current Player: #" + game.getCurrentPlayerIndex() + " [ " + getPlayerLifepointsHTML(0) + " : " + getPlayerLifepointsHTML(1) + " ]");
        playerCardFrontend1.setEnabled(isOwnTurn);
        playerCardFrontend2.setEnabled(false);
        playerCardFrontend1.update(game.getPlayer(playerIndex));
        playerCardFrontend2.update(game.getPlayer(playerIndex).getEnemy());
        btnEndTurn.setEnabled(game.isRunning() && isOwnTurn);
        updateUI();
    }
    
    private String getPlayerLifepointsHTML(int playerIndex){
        return "<span style=\"color:" + ((playerIndex == 0)?"#00FF00":"#FF0000") + ";\">" + game.getPlayer(playerIndex).getLifepoints() + "</span>";
    }
    
    private void initialiseCardPanel_Deck(PlayerCardFrontend playerCardFrontend, JPanel panContainer){
        PanCard_Deck panCard = new PanCard_Deck();
        panCard.setSize(cardWidth, cardHeight);
        panCard.setLocation((5 * cardWidth) + (6 * padding), padding);
        panContainer.add(panCard);
        playerCardFrontend.setPanCard_Deck(panCard);
    }
    
    private void initialiseCardPanel_Graveyard(PlayerCardFrontend playerCardFrontend, JPanel panContainer, int y){
        PanCard panCard = new PanCard();
        panCard.setSize(cardWidth, cardHeight);
        panCard.setLocation((5 * cardWidth) + (6 * padding), y);
        panContainer.add(panCard);
        playerCardFrontend.setPanCard_Graveyard(panCard);
    }
    
    public void initaliseCardPanels_Hand(PlayerCardFrontend playerCardFrontend, int playerIndex, int amount){
        JPanel panContainer = ((playerIndex == this.playerIndex)?panHand1:panHand2);
        PanCard[] panCards = initaliseCardPanelsRow(panContainer, amount, padding);
        playerCardFrontend.setPanCards_Hand(panCards);
    }
    
    public void initaliseCardPanels_Monsters(PlayerCardFrontend playerCardFrontend, int playerIndex, int amount){
        JPanel panContainer = panField1;
        int y = padding;
        if(playerIndex != this.playerIndex){
            panContainer = panField2;
            y = (padding + cardHeight + padding);
        }
        PanCard[] panCards = initaliseCardPanelsRow(panContainer, amount, y);
        playerCardFrontend.setPanCards_Monsters(panCards);
    }
    
    public void initaliseCardPanels_ManaPool(PlayerCardFrontend playerCardFrontend, int playerIndex, int amount){
        JPanel panContainer = panField1;
        int y = (padding + cardHeight + padding);
        if(playerIndex != this.playerIndex){
            panContainer = panField2;
            y = padding;
        }
        PanCard[] panCards = initaliseCardPanelsRow(panContainer, amount, y);
        playerCardFrontend.setPanCards_ManaPool(panCards);
    }
    
    private PanCard[] initaliseCardPanelsRow(JPanel panContainer, int amount, int y){
        PanCard[] panCards = new PanCard[amount];
        float stepX = (cardWidth + padding);
        if(amount > 5){
            int width = ((5 * cardWidth) + (4 * padding));
            stepX = (cardWidth - ((((float) amount * cardWidth) - width) / (amount - 1)));
        }
        float x = padding;
        for(int i=0;i<amount;i++){
            PanCard panCard = new PanCard();
            panCard.setSize(cardWidth, cardHeight);
            panCard.setLocation((int) x, y);
            panContainer.add(panCard);
            x += stepX;
            panCards[i] = panCard;
        }
        return panCards;
    }

    public int getPlayerIndex(){
        return playerIndex;
    }

    /**
     * This method is called from within the constructor to initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is always
     * regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        panHand2 = new javax.swing.JPanel();
        panField2 = new javax.swing.JPanel();
        panField1 = new javax.swing.JPanel();
        panHand1 = new javax.swing.JPanel();
        jPanel5 = new javax.swing.JPanel();
        btnEndTurn = new javax.swing.JButton();
        lblCurrentPlayer = new javax.swing.JLabel();
        btnCancelCardChooser = new javax.swing.JButton();

        panHand2.setBackground(new java.awt.Color(102, 102, 102));

        javax.swing.GroupLayout panHand2Layout = new javax.swing.GroupLayout(panHand2);
        panHand2.setLayout(panHand2Layout);
        panHand2Layout.setHorizontalGroup(
            panHand2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGap(0, 0, Short.MAX_VALUE)
        );
        panHand2Layout.setVerticalGroup(
            panHand2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGap(0, 80, Short.MAX_VALUE)
        );

        panField2.setBackground(new java.awt.Color(102, 102, 102));

        javax.swing.GroupLayout panField2Layout = new javax.swing.GroupLayout(panField2);
        panField2.setLayout(panField2Layout);
        panField2Layout.setHorizontalGroup(
            panField2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGap(0, 0, Short.MAX_VALUE)
        );
        panField2Layout.setVerticalGroup(
            panField2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGap(0, 150, Short.MAX_VALUE)
        );

        panField1.setBackground(new java.awt.Color(102, 102, 102));

        javax.swing.GroupLayout panField1Layout = new javax.swing.GroupLayout(panField1);
        panField1.setLayout(panField1Layout);
        panField1Layout.setHorizontalGroup(
            panField1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGap(0, 0, Short.MAX_VALUE)
        );
        panField1Layout.setVerticalGroup(
            panField1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGap(0, 150, Short.MAX_VALUE)
        );

        panHand1.setBackground(new java.awt.Color(102, 102, 102));

        javax.swing.GroupLayout panHand1Layout = new javax.swing.GroupLayout(panHand1);
        panHand1.setLayout(panHand1Layout);
        panHand1Layout.setHorizontalGroup(
            panHand1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGap(0, 0, Short.MAX_VALUE)
        );
        panHand1Layout.setVerticalGroup(
            panHand1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGap(0, 80, Short.MAX_VALUE)
        );

        jPanel5.setBackground(new java.awt.Color(153, 153, 153));

        btnEndTurn.setText("End Turn");
        btnEndTurn.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnEndTurnActionPerformed(evt);
            }
        });

        lblCurrentPlayer.setText("?");

        btnCancelCardChooser.setText("Cancel");

        javax.swing.GroupLayout jPanel5Layout = new javax.swing.GroupLayout(jPanel5);
        jPanel5.setLayout(jPanel5Layout);
        jPanel5Layout.setHorizontalGroup(
            jPanel5Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, jPanel5Layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(lblCurrentPlayer, javax.swing.GroupLayout.DEFAULT_SIZE, 210, Short.MAX_VALUE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(btnCancelCardChooser)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(btnEndTurn)
                .addContainerGap())
        );
        jPanel5Layout.setVerticalGroup(
            jPanel5Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, jPanel5Layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(jPanel5Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                    .addComponent(lblCurrentPlayer, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                    .addGroup(jPanel5Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                        .addComponent(btnEndTurn, javax.swing.GroupLayout.DEFAULT_SIZE, 28, Short.MAX_VALUE)
                        .addComponent(btnCancelCardChooser, javax.swing.GroupLayout.DEFAULT_SIZE, 28, Short.MAX_VALUE)))
                .addContainerGap())
        );

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(this);
        this.setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addComponent(panHand2, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
            .addComponent(panHand1, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
            .addComponent(panField1, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
            .addComponent(jPanel5, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
            .addComponent(panField2, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addComponent(panHand2, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(panField2, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jPanel5, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(panField1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(panHand1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
        );
    }// </editor-fold>//GEN-END:initComponents

    private void btnEndTurnActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnEndTurnActionPerformed
        if(game.getPlayer(game.getCurrentPlayerIndex()).getHand().size() > game.getGameMode().getMaximumHandCards(game.getCurrentPlayer())){
            cardChooserManager.startChoosing_ThrowingOff(game.getCurrentPlayerIndex());
        }
        commandSender.sendCommand(new EndTurnCommand());
    }//GEN-LAST:event_btnEndTurnActionPerformed

    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton btnCancelCardChooser;
    private javax.swing.JButton btnEndTurn;
    private javax.swing.JPanel jPanel5;
    private javax.swing.JLabel lblCurrentPlayer;
    private javax.swing.JPanel panField1;
    private javax.swing.JPanel panField2;
    private javax.swing.JPanel panHand1;
    private javax.swing.JPanel panHand2;
    // End of variables declaration//GEN-END:variables
}
